import { createSlice } from '@reduxjs/toolkit';
import http from '@/utils/request';
import { setToken as _setToken, getToken } from '@/utils/token';
const userStore = createSlice({
	name: 'user',
	initialState: {
		token: getToken() || '',
		userInfo: {},
	},
	reducers: {
		setToken: (state, { payload }) => {
			state.token = payload;
		},
		setUserInfo: (state, { payload }) => {
			state.userInfo = payload;
		},
	},
});
export const { setToken, setUserInfo } = userStore.actions;

const fetLogin = (loginForm) => {
	return async (dispatch) => {
		const res = await http.post('/authorizations', loginForm);
		dispatch(setToken(res.data.token));
		_setToken(res.data.token);
	};
};
export { fetLogin };
export default userStore.reducer;
